package org.southyin.controller;

import com.fasterxml.jackson.databind.ObjectMapper;
import org.southyin.commons.CommonsPage;
import org.southyin.commons.CommonsResp;
import org.southyin.model.User;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@WebServlet(value = "/login2")
public class LoginServlet2 extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        // 处理中文乱码，数据响应类型
        req.setCharacterEncoding("utf-8");
        resp.setCharacterEncoding("utf-8");
        resp.setContentType("application/json;charset=utf-8");

        // 用户信息
        User user1 = new User(101,"zhangsan1");
        User user2 = new User(102,"zhangsan2");
        User user3 = new User(103,"zhangsan3");
        User user4 = new User(104,"zhangsan4");
        User user5 = new User(105,"zhangsan5");
        List<User> list = new ArrayList<User>();
        list.add(user1);
        list.add(user2);
        list.add(user3);
        list.add(user4);
        list.add(user5);
        // 分页信息
        CommonsPage commonsPage = new CommonsPage();
        commonsPage.setTotal(100);
        commonsPage.setData(list);
        resp.getWriter().println(new ObjectMapper().writeValueAsString(commonsPage));
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String id = req.getParameter("id");
        System.out.println(id);
    }

}
